<?php
class Admin_Polls extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('categories_m');
	}
	public function index(){
		$model = $this->categories_m->get_all();
		$this->template
			->set_breadcrumb(lang('polls:polls'))
			->set('action',$this->action)
			->set('model',$model)
			->set('help',lang('polls:help'))
			->set('url',site_url($this->uri->uri_string()))
			->build($this->controller.'/'.$this->method);
	}
	
	public function create(){
		
		$model = $this->input->post();
		
		$this->form_validation->set_rules('title', lang('global:title'), 'required');
		
		$type =  substr($this->input->post('btnAction'), 0);
		
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); 
					}
				}
				$id_insert = $this->categories_m->insert($model);
				$this->redirect($this->method,$type,$id_insert);
			}
			
		}
		$this->template
			->set_breadcrumb(lang('polls:add_categories'));
		$this->view(array('model'=>$model));
	}
	
	public function edit($id){
		
		$model =(array)$this->categories_m->get($id);
		
		$this->form_validation->set_rules('title', lang('global:title'), 'required');
		
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$model = $this->input->post();
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); 
					}
				}
				$this->categories_m->update($id,$model);
				$this->redirect($this->method,$type,$id);
			}
			
		}
		
		$this->template
			->set_breadcrumb(lang('polls:add_categories'));
		$this->view(array('model'=>$model));
	}
	
	//
	function home( $id ){
		$this->categories_m->home( $id );
		redirect('admin/'.$this->module);
	}
}